<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="模板名称">
              <a-input placeholder="请输入模板名称" v-model="queryParam.templateName" :maxLength="128"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left; overflow: hidden" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button
        v-has="'ArchiveQualityTemplateList:add'"
        @click="
          editId = ''
          showEditModal = true
        "
        type="primary"
        icon="plus"
        >添加模板</a-button
      >
      <a-dropdown>
        <a-menu slot="overlay" @click="handleImport">
          <a-menu-item key="mode">导入模版下载</a-menu-item>
          <a-menu-item key="product">导入文件</a-menu-item>
        </a-menu>
        <a-button v-has="'ArchiveQualityTemplateList:import'" style="margin-left: 8px">
          批量导入 <a-icon type="down" />
        </a-button>
      </a-dropdown>
      <a-upload
        style="display: none"
        name="file"
        :showUploadList="false"
        :multiple="false"
        :headers="tokenHeader"
        :action="importExcelUrl"
        @change="handleImportExcel"
        ref="uploadExcel"
      ></a-upload>

      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item v-has="'ArchiveQualityTemplateList:del'" key="1" @click="batchDel"
            ><a-icon type="delete" />删除</a-menu-item
          >
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择
        <a style="font-weight: 600">{{ selectedRowKeys.length }}</a
        >项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        class="j-table-force-nowrap"
        :scroll="{ x: true }"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{ selectedRowKeys: selectedRowKeys, onChange: onSelectChange }"
        @change="handleTableChange"
      >
        <span slot="action" slot-scope="text, record">
          <a
            v-has="'ArchiveQualityTemplateList:edit'"
            @click="
              editId = record.id
              showEditModal = true
            "
            >编辑</a
          >

          <a-divider type="vertical" />
          <a-dropdown>
            <a class="ant-dropdown-link">更多 <a-icon type="down" /></a>
            <a-menu slot="overlay">
              <a-menu-item v-has="'ArchiveQualityTemplateList:export'">
                <a @click="handleExportXls(record.templateName, url.exportXlsUrl, { id: record.id })">导出</a>
              </a-menu-item>
              <a-menu-item v-has="'ArchiveQualityTemplateList:del'">
                <a-popconfirm title="确定删除吗?" @confirm="() => handleDelete(record.id)">
                  <a>删除</a>
                </a-popconfirm>
              </a-menu-item>
              <!-- 日志 -->
              <a-menu-item v-has="'ArchiveQualityTemplateList:log'">
                <j-operate-log :id="record.id" />
              </a-menu-item>
            </a-menu>
          </a-dropdown>
        </span>
      </a-table>
    </div>

    <quality-template-modal v-if="showEditModal" :visible.sync="showEditModal" :editId="editId" @submit="modalFormOk" />
  </a-card>
</template>

<script>
import { JeecgListMixin } from '@/mixins/JeecgListMixin'
import QualityTemplateModal from './modules/QualityTemplateModal.vue'
import '@/assets/less/TableExpand.less'

export default {
  name: 'ArchiveQualityTemplateList',
  mixins: [JeecgListMixin],
  components: {
    QualityTemplateModal,
  },
  data() {
    return {
      showEditModal: false, // 是否显示编辑弹框
      editId: '', //  要编辑或查看的产品档案id，为空表示新增
      // 表头
      columns: [
        {
          title: '序号',
          dataIndex: '',
          key: 'rowIndex',
          width: 60,
          align: 'center',
          customRender: function (t, r, index) {
            return parseInt(index) + 1
          },
        },
        {
          title: '模板名称',
          align: 'center',
          dataIndex: 'templateName',
        },
        {
          title: '创建',
          align: 'center',
          dataIndex: 'creator',
        },
        {
          title: '变更',
          align: 'center',
          customRender(text, record) {
            return record.updateTime ? record.updateTime + ';' + record.updateBy : '无变更记录'
          },
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          fixed: 'right',
          width: 147,
          scopedSlots: { customRender: 'action' },
        },
      ],
      url: {
        list: '/hzkj-product/product/archive/hzArchiveQualityTemplate/list',
        delete: '/hzkj-product/product/archive/hzArchiveQualityTemplate/delete',
        deleteBatch: '/hzkj-product/product/archive/hzArchiveQualityTemplate/deleteBatch',
        exportXlsUrl: '/hzkj-product/product/archive/hzArchiveQualityTemplate/exportXls',
        importExcelUrl: 'hzkj-product/product/archive/hzArchiveQualityTemplate/importExcel',
        downloadTemplate: '/hzkj-product/product/archive/hzArchiveQualityTemplate/exportXlsTemplatesXls',
      },
      dictOptions: {},
      superFieldList: [],
    }
  },
  created() {
    this.getSuperFieldList()
  },
  computed: {
    importExcelUrl: function () {
      return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`
    },
  },
  methods: {
    getSuperFieldList() {
      let fieldList = []
      fieldList.push({ type: 'string', value: 'templateName', text: '模板名称', dictCode: '' })
      fieldList.push({ type: 'string', value: 'creator', text: '创建', dictCode: '' })
      fieldList.push({ type: 'string', value: 'updator', text: '变更', dictCode: '' })
      this.superFieldList = fieldList
    },
    // 批量导入
    handleImport({ key }) {
      if (key == 'mode') {
        //模板下载
        this.handleExportXls('质检模板', this.url.downloadTemplate, {})
      } else {
        //导入新产品
        this.$refs.uploadExcel.$el.querySelector('input[type="file"]').click()
      }
    },
  },
}
</script>
<style scoped>
@import '~@assets/less/common.less';
</style>
